package training_camp.day2.leetcode59;

class Solution {
    private static final int[][] DIR = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};

    public int[][] generateMatrix(int n) {
        int[][] ints = new int[n][n];
        int i = 0, j = 0;
        int di = 0;
        for (int val = 1; val <= n * n; val++) {
            ints[i][j] = val;
            int x = i + DIR[di][0];
            int y = j + DIR[di][1];
            if (x >= n || x < 0 || y >= n || y < 0 || ints[x][y] != 0) {
                di = (di + 1) % 4;
            }
            i = i + DIR[di][0];
            j = j + DIR[di][1];
        }
        return ints;
    }
}